package com.youmingxing.view.clipheadphoto.clip1;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.RelativeLayout;

import com.youmingxing.utils.DisplayUtil;
import com.youmingxing.utils.LogUtil;
import com.youmingxing.view.clipheadphoto.clip1.msg.BaseMessage;
import com.youmingxing.view.clipheadphoto.clip1.msg.MessageCenter;
import com.youmingxing.view.clipheadphoto.clip1.msg.MessageListenerDelegate;
import com.youmingxing.view.clipheadphoto.clip1.msg.MessageTable;
import com.youmingxing.view.clipheadphoto.clip1.msg.MessageWithFloat;

public class ClipImageLayout extends RelativeLayout implements MessageListenerDelegate {

    private ClipZoomImageView mZoomImageView;
    private ClipImageBorderView mClipImageView;

    private int mHorizontalPadding = 0;

    public ClipImageLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        MessageCenter.getInstance().registerListener(this, MessageTable.RefreshView);
        mZoomImageView = new ClipZoomImageView(context);
        mClipImageView = new ClipImageBorderView(context);

        android.view.ViewGroup.LayoutParams lp = new LayoutParams(
                android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                android.view.ViewGroup.LayoutParams.MATCH_PARENT);

        this.addView(mZoomImageView, lp);


//        layoutParams.width =   DisplayUtil.getScreenWidth();
//        layoutParams.width =   DisplayUtil.getScreenWidth();

        RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(
                DisplayUtil.getScreenWidth(),
                LayoutParams.WRAP_CONTENT);
        layout.setMargins(0, 0, 0, 0);
        mZoomImageView.setLayoutParams(layout);


//        mZoomImageView.setLayoutParams(layout);
//        mZoomImageView.setAdjustViewBounds(true);
//        mClipImageView.setLayoutParams(new RelativeLayout.LayoutParams(
//                DisplayUtil.getScreenWidth(),
//               ));
        this.addView(mClipImageView, lp);
        RelativeLayout.LayoutParams linearParams = (RelativeLayout.LayoutParams)
                mClipImageView.getLayoutParams(); //
        linearParams.height = LayoutParams.MATCH_PARENT;
        mClipImageView.setLayoutParams(linearParams);
        // 计算padding的px
        mHorizontalPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                mHorizontalPadding, getResources().getDisplayMetrics());
        mZoomImageView.setHorizontalPadding(mHorizontalPadding);
        mClipImageView.setHorizontalPadding(mHorizontalPadding);


//        setBackgroundResource(R.color.red_global);
    }

    float scale = -1;

    public void setmZoomImageView(float scale) {
        RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(
                DisplayUtil.getScreenWidth(),
                DisplayUtil.getScreenWidth() * 9 / 16);
        layout.setMargins(0, 90, 0, 0);
        mZoomImageView.setLayoutParams(layout);
        mZoomImageView.isSetSame(scale);
        mClipImageView.setmZoomImageView(scale);
        this.scale = scale;
//        mClipImageView.setBig(true);

    }

    float Hight = 100;

    public void setsetHight(float Hight) {
        this.Hight = Hight;

    }

    /**
     * 对外公布设置边距的方法,单位为dp
     *
     * @param mHorizontalPadding
     */
    public void setHorizontalPadding(int mHorizontalPadding) {
        this.mHorizontalPadding = mHorizontalPadding;
    }

    /**
     * 裁切图片
     *
     * @return
     */
    public Bitmap clip(boolean isWidthBig) {
        return mZoomImageView.clip(isWidthBig);
    }

    public void setBitmap(Bitmap bitmap) {
        mZoomImageView.setImageBitmap(bitmap);
    }


    public void reDraw(float movePos) {
        mClipImageView.reDraw(movePos);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        MessageCenter.getInstance().removeListenerFromMessage(this, MessageTable.RefreshView);
    }

    @Override
    public void onListenerExit() {

    }

    private int movePos = 0;

    @Override
    public void getMessage(BaseMessage message) {
        if (Hight - DisplayUtil.getScreenWidth()-200 > 0) {
            if (message.getMsgId().equals(MessageTable.RefreshView)) {
                if ((int) ((MessageWithFloat) message).getFloat() < 0) {  //  往上滑动
//            mClipImageView.reDraw(movePos);
                    if (movePos <= 0 && -(movePos + (int) ((MessageWithFloat) message).getFloat()) < 600) {
                        LogUtil.i("--------up--1-", "down");

                        movePos = movePos + (int) ((MessageWithFloat) message).getFloat();
                        mZoomImageView.setPadding(mZoomImageView.getPaddingLeft(), movePos * 2 / 3,
                                mZoomImageView.getPaddingRight(), 120 - movePos);

                    } else if (movePos < 0 && -((movePos + (int) ((MessageWithFloat) message).getFloat())) >= 600) {
                        LogUtil.i("--------up--2-", "down");
//                    RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(
//                            DisplayUtil.getScreenWidth(),
//                            DisplayUtil.getScreenWidth() * 9 / 16+90);
//                    layout.setMargins(0, 0, 0, 0);
//                    mZoomImageView.setLayoutParams(layout);
//                    movePos = movePos + (int) ((MessageWithFloat) message).getFloat();

                    } else if (movePos >= 0 && movePos + (int) ((MessageWithFloat) message).getFloat() <= 190) {
                        LogUtil.i("--------up--3-", "down");

                        movePos = movePos + (int) ((MessageWithFloat) message).getFloat();
                        mZoomImageView.setPadding(mZoomImageView.getPaddingLeft(), movePos,
                                mZoomImageView.getPaddingRight(), movePos * 2 / 3);
                    } else if (movePos + (int) ((MessageWithFloat) message).getFloat() > 190) {
                        LogUtil.i("--------up--4-", "down");

                        movePos = movePos + (int) ((MessageWithFloat) message).getFloat();
                        mZoomImageView.setPadding(mZoomImageView.getPaddingLeft(), 190,
                                mZoomImageView.getPaddingRight(), movePos * 2 / 3);
                    }
                } else {    //  down
                    if (movePos < 0 && -(movePos + (int) ((MessageWithFloat) message).getFloat()) < 600) {
                        LogUtil.i("--------down--1-", "down");
//                    movePos = movePos + (int) ((MessageWithFloat) message).getFloat();
//                    mZoomImageView.setPadding(mZoomImageView.getPaddingLeft(), movePos * 2 / 3,
//                            mZoomImageView.getPaddingRight(), 240 - movePos);

                        movePos = movePos + (int) ((MessageWithFloat) message).getFloat();
                        mZoomImageView.setPadding(mZoomImageView.getPaddingLeft(), movePos * 2 / 3,
                                mZoomImageView.getPaddingRight(), 120 - movePos);
                    } else if (movePos < 0 && -movePos >= 600) {
                        LogUtil.i("--------down--2-", "down");

                        movePos = movePos + (int) ((MessageWithFloat) message).getFloat();
                        mZoomImageView.setPadding(mZoomImageView.getPaddingLeft(), movePos * 2 / 3,
                                mZoomImageView.getPaddingRight(), 120 - movePos);

//                    movePos = movePos + (int) ((MessageWithFloat) message).getFloat();
//                    mZoomImageView.setPadding(mZoomImageView.getPaddingLeft(), -100,
//                            mZoomImageView.getPaddingRight(), 360);
                    } else if (movePos >= 0 && movePos + (int) ((MessageWithFloat) message).getFloat() <= 10) {
                        movePos = movePos + (int) ((MessageWithFloat) message).getFloat();
                        mZoomImageView.setPadding(mZoomImageView.getPaddingLeft(), movePos,
                                mZoomImageView.getPaddingRight(), movePos * 2 / 3);
                    } else if (movePos + (int) ((MessageWithFloat) message).getFloat() > 10) {
                        LogUtil.i("--------down--4-", "down");
                        mZoomImageView.setPadding(mZoomImageView.getPaddingLeft(), 10,
                                mZoomImageView.getPaddingRight(), movePos * 2 / 3);
                    }
                }
            }
        } else {




            if (message.getMsgId().equals(MessageTable.RefreshView)) {
                if ((int) ((MessageWithFloat) message).getFloat() < 0) {  //  往上滑动
//            mClipImageView.reDraw(movePos);
                    if (movePos <= 0 && -(movePos + (int) ((MessageWithFloat) message).getFloat()) < Hight  - (DisplayUtil.getScreenWidth() * 9 / 16)) {
                        LogUtil.i("--------up--1-", "down");

                        movePos = movePos + (int) ((MessageWithFloat) message).getFloat();
                        mZoomImageView.setPadding(mZoomImageView.getPaddingLeft(), movePos * 2 / 3,
                                mZoomImageView.getPaddingRight(), 120 - movePos);

                    } else if (movePos < 0 && -((movePos + (int) ((MessageWithFloat) message).getFloat())) >= Hight  - (DisplayUtil.getScreenWidth() * 9 / 16)) {
                        LogUtil.i("--------up--2-", "down");
//                    RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(
//                            DisplayUtil.getScreenWidth(),
//                            DisplayUtil.getScreenWidth() * 9 / 16+90);
//                    layout.setMargins(0, 0, 0, 0);
//                    mZoomImageView.setLayoutParams(layout);
//                    movePos = movePos + (int) ((MessageWithFloat) message).getFloat();

                    } else if (movePos >= 0 && movePos + (int) ((MessageWithFloat) message).getFloat() <= 190) {
                        LogUtil.i("--------up--3-", "down");

                        movePos = movePos + (int) ((MessageWithFloat) message).getFloat();
                        mZoomImageView.setPadding(mZoomImageView.getPaddingLeft(), movePos,
                                mZoomImageView.getPaddingRight(), movePos * 2 / 3);
                    } else if (movePos + (int) ((MessageWithFloat) message).getFloat() > 190) {
                        LogUtil.i("--------up--4-", "down");

                        movePos = movePos + (int) ((MessageWithFloat) message).getFloat();
                        mZoomImageView.setPadding(mZoomImageView.getPaddingLeft(), 190,
                                mZoomImageView.getPaddingRight(), movePos * 2 / 3);
                    }
                } else {    //  down
                    if (movePos < 0 && -(movePos + (int) ((MessageWithFloat) message).getFloat()) <  Hight - 90 - (DisplayUtil.getScreenWidth() * 9 / 16)) {
                        LogUtil.i("--------down--1-", "down");
//                    movePos = movePos + (int) ((MessageWithFloat) message).getFloat();
//                    mZoomImageView.setPadding(mZoomImageView.getPaddingLeft(), movePos * 2 / 3,
//                            mZoomImageView.getPaddingRight(), 240 - movePos);

                        movePos = movePos + (int) ((MessageWithFloat) message).getFloat();
                        mZoomImageView.setPadding(mZoomImageView.getPaddingLeft(), -movePos ,
                                mZoomImageView.getPaddingRight(),   -movePos * 2 / 3);
                    } else if (movePos < 0 && -movePos >=  Hight - 90 - (DisplayUtil.getScreenWidth() * 9 / 16)) {
                        LogUtil.i("--------down--2-", "down");

                        movePos = movePos + (int) ((MessageWithFloat) message).getFloat();
                        mZoomImageView.setPadding(mZoomImageView.getPaddingLeft(), -movePos * 2 / 3,
                                mZoomImageView.getPaddingRight(), 120 - movePos);

//                    movePos = movePos + (int) ((MessageWithFloat) message).getFloat();
//                    mZoomImageView.setPadding(mZoomImageView.getPaddingLeft(), -100,
//                            mZoomImageView.getPaddingRight(), 360);
                    } else if (movePos >= 0 && movePos + (int) ((MessageWithFloat) message).getFloat() <= 190) {
                        LogUtil.i("--------down--3-", "down");
                        movePos = movePos + (int) ((MessageWithFloat) message).getFloat();
                        mZoomImageView.setPadding(mZoomImageView.getPaddingLeft(), movePos,
                                mZoomImageView.getPaddingRight(), movePos * 2 / 3);
                    }
                }
            }


        }


    }
}

